package com.siwei.apply.controller;

import com.siwei.apply.mapper.NodeLandMapper;
import com.siwei.apply.service.NodeLandService;
import com.siwei.common.core.domain.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * NodeLand 调试控制器
 * 用于调试和排查 shppath 返回问题
 * 
 * @author siwei-zhx
 */
@RestController
@RequestMapping("/debug/nodeland")
public class NodeLandDebugController {

    private static final Logger logger = LoggerFactory.getLogger(NodeLandDebugController.class);

    @Autowired
    private NodeLandMapper nodeLandMapper;

    @Autowired
    private NodeLandService nodeLandService;

    /**
     * 直接调用 Mapper 查询，查看原始返回数据
     */
    @GetMapping("/mapper/{nodeId}")
    public R<Map<String, String>> testMapper(@PathVariable String nodeId) {
        try {
            logger.info("调试：直接调用 Mapper 查询，nodeId: {}", nodeId);
            
            Map<String, String> result = nodeLandMapper.selectGeomByNodeId(nodeId);
            
            logger.info("调试：Mapper 返回结果: {}", result);
            
            if (result == null) {
                return R.fail("未查询到数据");
            }
            
            return R.ok(result);
            
        } catch (Exception e) {
            logger.error("调试：Mapper 查询异常", e);
            return R.fail("查询异常：" + e.getMessage());
        }
    }

    /**
     * 调用 Service 查询，查看处理后的数据
     */
    @GetMapping("/service/{nodeId}")
    public R<Map<String, Object>> testService(@PathVariable String nodeId) {
        try {
            logger.info("调试：调用 Service 查询，nodeId: {}", nodeId);
            
            Map<String, Object> result = nodeLandService.getGeomByNodeId(nodeId);
            
            logger.info("调试：Service 返回结果: {}", result);
            
            if (result == null) {
                return R.fail("未查询到数据");
            }
            
            return R.ok(result);
            
        } catch (Exception e) {
            logger.error("调试：Service 查询异常", e);
            return R.fail("查询异常：" + e.getMessage());
        }
    }

    /**
     * 检查数据库中是否存在相关数据
     */
    @GetMapping("/check/{nodeId}")
    public R<Map<String, Object>> checkData(@PathVariable String nodeId) {
        try {
            logger.info("调试：检查数据库数据，nodeId: {}", nodeId);
            
            // 这里可以添加更多的检查逻辑
            Map<String, String> mapperResult = nodeLandMapper.selectGeomByNodeId(nodeId);
            Map<String, Object> serviceResult = nodeLandService.getGeomByNodeId(nodeId);
            
            Map<String, Object> checkResult = new java.util.HashMap<>();
            checkResult.put("mapperResult", mapperResult);
            checkResult.put("serviceResult", serviceResult);
            checkResult.put("mapperHasShppath", mapperResult != null && mapperResult.containsKey("shppath"));
            checkResult.put("serviceHasShppath", serviceResult != null && serviceResult.containsKey("shppath"));
            checkResult.put("shppathValue", mapperResult != null ? mapperResult.get("shppath") : null);
            
            logger.info("调试：检查结果: {}", checkResult);
            
            return R.ok(checkResult);
            
        } catch (Exception e) {
            logger.error("调试：数据检查异常", e);
            return R.fail("检查异常：" + e.getMessage());
        }
    }
}
